﻿namespace com.stifel.ServerBuildMgmtDao
{
    using System;
    using System.Linq;

    using com.stifel.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = StifelServerBuildDataContext;

    using CustomObject = StifelNotification;

    public class StifelNotificationDao : StifelBaseDao
    {
        #region Fields

        const string ListName = "stifelnotification";

        #endregion Fields

        #region Constructors

        public StifelNotificationDao( )
            : base(ListName)
        {
            //CurrentWebUrl = SPContext.Current.Web.Url;
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelNotificationDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public static bool ListExists( string siteUrl )
        {
            bool ok = false;
            try
            {
                using ( SPSite spSite = new SPSite( siteUrl ) )
                {
                    string s = siteUrl.Replace( spSite.Url, string.Empty );
                    s = s.StartsWith( "/" ) ? s.Substring( 1 ) : s;
                    using ( SPWeb spWeb = spSite.OpenWeb( s ) )
                    {
                        ok = spWeb.Lists.ContainsList( ListName );
                    }
                }
            }
            catch
            {
            }
            return ok;
        }

        public Guid Add( CustomObject value )
        {
            Guid guid = Guid.NewGuid( );
            Guid uniqueId = Guid.Empty;
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject notification = clean( value );
                string title = notification.Title;
                notification.Title = guid.ToString( );
                l.InsertOnSubmit( notification );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                         where o.Title == guid.ToString( )
                                         select o ).SingleOrDefault( );
                v.Title = title;
                uniqueId = v.UniqueId;
                c.SubmitChanges( );
            }

            return uniqueId;
        }

        public void Delete( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                         where o.UniqueId == uniqueId
                                         select o ).SingleOrDefault( );
                if ( v != null )
                {
                    l.DeleteOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void Delete( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                         where o.Id == id
                                         select o ).SingleOrDefault( );
                if ( v != null )
                {
                    l.DeleteOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public CustomObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                         where o.Id == id
                                         select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject Get( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                         where o.UniqueId == uniqueId
                                         select o ).SingleOrDefault( );
                return v;
            }
        }

        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                Notification = value.Notification,
                NotificationURL = value.NotificationURL,
                RequestIdentifier = value.RequestIdentifier,
                RequestUniqueId = value.RequestUniqueId,
                Title = string.IsNullOrEmpty( value.Title ) ? value.Notification.Length > 255 ? value.Notification.Substring( 0, 255 ) : value.Notification : value.Title
            };
        }

        #endregion Methods
    }
}